﻿using System;
using System.Collections.Generic;
using App_Code.Common;
using App_Code.Helpers;
using App_Code.Objects;

namespace App_Code.Logic
{
    /// <summary>
    /// Summary description for GridDataSource
    /// </summary>
    public class GridDataSource
    {

        public static List<JobDTO> GetJobsStub(int JobCount)
        {
            return GenerateStub(JobCount + Constants.JOB_GRID_RESERVE_COUNT);
        }

        public static List<JobDTO> GetJobs(string userServerId)
        {
            if (PageHelper.VisualCronSession.Servers.Exists(s => s.UserServerId == userServerId))
            {
                var list = PageHelper.VisualCronSession.GetJobsDTO(userServerId);
                list.Sort((x, y) => String.Compare(x.JobName, y.JobName));
                return list;
            }
            return null;
        }

        public static List<TaskDTO> GetTasks(string userServerId, string jobId)
        {
            if (PageHelper.VisualCronSession.Servers.Exists(s => s.UserServerId == userServerId))
            {
                var list = PageHelper.VisualCronSession.GetTasksDTO(userServerId,
                                                                jobId);
                list.Sort((x, y) => x.Order.CompareTo(y.Order));
                return list;
            }

            return null;
        }

        #region Private methods

        private static List<JobDTO> GenerateStub(int size)
        {
            var stub = new List<JobDTO>(size);
            for (int i = 0; i < size; i++)
            {
                stub.Add(new JobDTO("", null));
            }

            return stub;
        }
        #endregion
    }
}